/*
 * @Author: xiaosihan 
 * @Date: 2024-07-01 17:29:35 
 * @Last Modified by: xiaosihan
 * @Last Modified time: 2024-07-02 19:56:32
 */
import { AbstractMesh, AssetContainer, IParticleSystem, Scene, Skeleton, AnimationGroup, TransformNode, Geometry, Light, ISpriteManager } from "@babylonjs/core";
import type { ISceneLoaderPluginAsync, ISceneLoaderAsyncResult, ISceneLoaderProgressEvent } from "@babylonjs/core/Loading/sceneLoader.js";
import { SceneLoader } from "@babylonjs/core/Loading/sceneLoader.js";

import { ColladaLoader } from 'three/examples/jsm/loaders/ColladaLoader.js';
import { GLTFExporter } from 'three/examples/jsm/exporters/GLTFExporter';

const colladaLoader = new ColladaLoader();


// dae 加载器
export class DAEFileLoader implements ISceneLoaderPluginAsync {

    constructor() {
    }

    name = "dae";

    extensions = {
        ".dae": { isBinary: false },
    };

    // 提供给babylon 调用的 加载dae的方法   
    async importMeshAsync(meshesNames: string | readonly string[] | null | undefined, scene: Scene, daeData: string, rootUrl: string) {

        const threeResult = await colladaLoader.parse(daeData, rootUrl);
        const gltfExporter = new GLTFExporter();
        gltfExporter.parse(
            threeResult.scene,
            (result) => {
                debugger;
                if (result instanceof ArrayBuffer) {
                    const blobUrl = new Blob([result], { type: 'application/octet-stream' });
                    debugger;
                }
            },
            (error: ErrorEvent) => {
                debugger;
            },
            {
                trs: true,
                onlyVisible: true, // 只导出可见的
                binary: true, // glb
                maxTextureSize: 4096,
                animations: threeResult.scene.animations
            }
        );

        const meshes: AbstractMesh[] = [];
        const particleSystems: IParticleSystem[] = [];
        const skeletons: Skeleton[] = [];
        const animationGroups: AnimationGroup[] = [];
        const transformNodes: TransformNode[] = [];
        const geometries: Geometry[] = [];
        const lights: Light[] = [];
        const spriteManagers: ISpriteManager[] = [];


        debugger;

        return new Promise<ISceneLoaderAsyncResult>((resolve, reject) => {
            resolve({
                meshes,
                particleSystems,
                skeletons,
                animationGroups,
                transformNodes,
                geometries,
                lights,
                spriteManagers,
            });

        });
    }

    async loadAsync(scene: Scene, data: unknown, rootUrl: string, onProgress?: (event: ISceneLoaderProgressEvent) => void, fileName?: string) {
        debugger;
    }

    async loadAssetContainerAsync(scene: Scene, data: unknown, rootUrl: string, onProgress?: (event: ISceneLoaderProgressEvent) => void, fileName?: string) {
        debugger;
        return new AssetContainer();
    }


}

if (SceneLoader) {
    SceneLoader.RegisterPlugin(new DAEFileLoader());
}